logo

Crate wkt

source · []
Expand description

The wkt crate provides conversions to and from the WKT (Well Known Text) geometry format.

Conversions are available via the TryFromWkt and ToWkt traits, with implementations for geo_types primitives enabled by default.

For advanced usage, see the types module for a list of internally used types.

This crate has optional serde integration for deserializing fields containing WKT. See deserialize for an example.

Examples

Read geo_types from a WKT string

// This example requires the geo-types feature (on by default).
use wkt::TryFromWkt;
use geo_types::Point;

let point: Point<f64> = Point::try_from_wkt_str("POINT(10 20)").unwrap();
assert_eq!(point.y(), 20.0);

Write geo_types to a WKT string

// This example requires the geo-types feature (on by default).
use wkt::ToWkt;
use geo_types::Point;

let point: Point<f64> = Point::new(1.0, 2.0);
assert_eq!(point.wkt_string(), "POINT(1 2)");

Read or write your own geometry types

Not using geo-types for your geometries? No problem!

You can use Wkt::from_str to parse a WKT string into this crate’s intermediate geometry structure. You can use that directly, or if have your own geometry types that you’d prefer to use, utilize that Wkt struct to implement the ToWkt or TryFromWkt traits for your own types.

In doing so, you’ll likely want to match on one of the WKT types (Point, Linestring, etc.) stored in its item field

use std::str::FromStr;
use wkt::Wkt;
use wkt::Geometry;

let wktls: Wkt<f64> = Wkt::from_str("LINESTRING(10 20, 20 30)").unwrap();
let ls = match wktls.item {
    Geometry::LineString(line_string) => {
        // you now have access to the `wkt::types::LineString`.
        assert_eq!(line_string.0[0].x, 10.0);
    }
    _ => unreachable!(),
};

Re-exports

pub use deserialize::deserialize_wkt;
pub use deserialize::geo_types::deserialize_geometry;
Deprecated
pub use deserialize::geo_types::deserialize_point;

Modules

conversionDeprecatedgeo-types

This module deserialises to WKT using serde.

This module provides conversions between WKT primitives and geo_types primitives.

WKT primitive types and collections

Structs

Container for WKT primitives and collections

Enums

All supported WKT geometry types

Traits

A trait for converting values to WKT

Create geometries from WKT.